package com.yxh.lib_base.ext

import android.os.Parcelable
import androidx.annotation.Nullable
import com.blankj.utilcode.util.GsonUtils
import com.tencent.mmkv.MMKV
import java.io.Serializable
import java.util.*

/**
 *@创建者：yuan_xh
 *@创建时间：2021/3/29 9:54
 *@描述：
 */
object KvUtil {
    private var mmkv: MMKV? = null
    fun getKv(): MMKV? {
        if (mmkv == null) {
            mmkv = MMKV.defaultMMKV()
        }
        return mmkv
    }

    fun encode(key: String, value: Any?) {
        when (value) {
            is String -> getKv()?.encode(key, value)
            is Float -> getKv()?.encode(key, value)
            is Boolean -> getKv()?.encode(key, value)
            is Int -> getKv()?.encode(key, value)
            is Long -> getKv()?.encode(key, value)
            is Double -> getKv()?.encode(key, value)
            is ByteArray -> getKv()?.encode(key, value)
            is Nothing -> return
        }
    }

    fun <T : Parcelable> encode(key: String, t: T?) {
        if (t == null) {
            return
        }
        getKv()?.encode(key, t)
    }

    fun <T : Serializable> encodeClass(key: String, t: T?) {
        if (t == null) {
            return
        }
        getKv()?.encode(key, GsonUtils.toJson(t))
    }

    fun encode(key: String, sets: Set<String>?) {
        if (sets == null) {
            return
        }
        getKv()?.encode(key, sets)
    }

    fun decodeInt(key: String, default: Int? = 0): Int {
        val def = default ?: 0
        return getKv()?.decodeInt(key, def) ?: def
    }

    fun decodeDouble(key: String): Double? {
        return getKv()?.decodeDouble(key, 0.00)
    }

    fun decodeLong(key: String, default: Long? = 0L): Long {
        return getKv()?.decodeLong(key, default?:0L) ?: default?:0L
    }

    fun decodeBoolean(key: String): Boolean {
        return getKv()?.decodeBool(key, false) ?: false
    }

    fun decodeBoolean(key: String, default: Boolean): Boolean {
        return getKv()?.decodeBool(key, default) ?: default
    }

    fun decodeFloat(key: String): Float? {
        return getKv()?.decodeFloat(key, 0F)
    }

    fun decodeByteArray(key: String): ByteArray? {
        return getKv()?.decodeBytes(key)
    }

    fun decodeString(key: String, default: String? = ""): String? {
        return getKv()?.decodeString(key, default)
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return getKv()?.decodeParcelable(key, tClass)
    }

    fun <T : Serializable> decodeClass(key: String, tClass: Class<T>): T? {
        val jsonStr = getKv()?.decodeString(key)
        return GsonUtils.fromJson(jsonStr, tClass)
    }

    fun <T : Serializable> decodeClass(
        key: String,
        tClass: Class<T>,
        @Nullable defaultValue: T
    ): T {
        val jsonStr = getKv()?.decodeString(key)
        return GsonUtils.fromJson(jsonStr, tClass) ?: defaultValue
    }


    fun decodeStringSet(key: String): Set<String>? {
        return getKv()?.decodeStringSet(key, Collections.emptySet())
    }

    fun removeKey(key: String) {
        getKv()?.removeValueForKey(key)
    }

    fun clearAll() {
        getKv()?.clearAll()
    }
}